﻿import flash.geom.Point;
import My3D.Face;
import My3D.Matrix3D;
import My3D.Point3D;
import My3D.Screen;
dynamic class My3D.Polygon extends Point3D {
	//--------------------------------------------------------------------------------- 
	//																					pirvate 
	public var tranceMatrix:Matrix3D;
	public var faces:Object;
	public var screen:Screen;
	public var polygonName:String;
	public var projectPointFun:Function;
	//--------------------------------------------------------------------------------- 
	//																					function
	public function Polygon(name:String) {
		if (name == undefined) {
			trace("not indetfind polygon name");
			return;
		}
		this.polygonName = name;
		this.tranceMatrix = new Matrix3D();
		this.faces = new Object();
		this.faces.all = new Array();
	}
	
	//-----------------------------------------------------------------------------------  MOVE
	
	/**
	 * move polygon to point
	 * @param	p
	 */
	public function MoveToP(p:Point3D) {
		this.MoveTo(p.x, p.y, p.z);
	}
	/**
	 * move polygon to point
	 * @param	x
	 * @param	y
	 * @param	z
	 */
	public function MoveTo(x:Number,y:Number,z:Number) {
		this.x = x;
		this.y = y;
		this.z = z;
	}
	/**
	 * move polygon offset
	 * @param	x
	 * @param	y
	 * @param	z
	 */
	public function Move(x:Number, y:Number, z:Number) {
		this.x += x;
		this.y += y;
		this.z += z;
	}
	
	//-----------------------------------------------------------------------------------  ROTATION
	/**
	 * rotation sub polygons or faces 
	 * @param	o
	 */
	public function RotationSubX(o:Number) {
		this.tranceMatrix = this.tranceMatrix.X(Matrix3D.getXRotation(o));
	}
	/**
	 * rotation sub polygons or faces 
	 * @param	o
	 */
	public function RotationSubY(o:Number) {
		this.tranceMatrix = this.tranceMatrix.X(Matrix3D.getYRotation(o));
	}
	/**
	 * rotation sub polygons or faces 
	 * @param	o
	 */
	public function RotationSubZ(o:Number) {
		this.tranceMatrix = this.tranceMatrix.X(Matrix3D.getZRotation(o));
	}
	
	//------------------------------------------------------------------------------------- TRANCFRAME
	/**
	 * rotation sub polygons or faces 
	 * @param	o
	 */
	public function Scale(o:Number) {
		this.tranceMatrix.xx *= o;
		this.tranceMatrix.yy *= o;
		this.tranceMatrix.zz *= o;
	}
	//------------------------------------------------------------------------------------- ADD TO SCREEN
	public function ProjectPoints() {
		
		if (this.projectPointFun == undefined) {
			for (var i:Number = 0;i<this.faces.all.length; i++ ) {
				var pa:Point = this.projectPoint(this.faces.all[i].$pointA);
				var pb:Point = this.projectPoint(this.faces.all[i].$pointB);
				var pc:Point = this.projectPoint(this.faces.all[i].$pointC);
				this.faces.all[i].$pointA.X = pa.x;
				this.faces.all[i].$pointA.Y = pa.y;
				this.faces.all[i].$pointA.Z = this.faces.all[i].$pointA.z;
				
				this.faces.all[i].$pointB.X = pb.x;
				this.faces.all[i].$pointB.Y = pb.y;
				this.faces.all[i].$pointB.Z = this.faces.all[i].$pointB.z;
				
				this.faces.all[i].$pointC.X = pc.x;
				this.faces.all[i].$pointC.Y = pc.y;
				this.faces.all[i].$pointC.Z = this.faces.all[i].$pointC.z;
			}
		}else {
			for (var i:Number = 0;i<this.faces.all.length; i++ ) {
				var pa:Point = this.projectPointFun(this.faces.all[i].$pointA);
				var pb:Point = this.projectPointFun(this.faces.all[i].$pointB);
				var pc:Point = this.projectPointFun(this.faces.all[i].$pointC);
				this.faces.all[i].$pointA.X = pa.x;
				this.faces.all[i].$pointA.Y = pa.y;
				this.faces.all[i].$pointA.Z = this.faces.all[i].$pointA.z;
				
				this.faces.all[i].$pointB.X = pb.x;
				this.faces.all[i].$pointB.Y = pb.y;
				this.faces.all[i].$pointB.Z = this.faces.all[i].$pointB.z;
				
				this.faces.all[i].$pointC.X = pc.x;
				this.faces.all[i].$pointC.Y = pc.y;
				this.faces.all[i].$pointC.Z = this.faces.all[i].$pointC.z;
			}
		}
	}
	//-------------------------------------------------------------------------------------------------
	public function projectPoint(p:Point3D):Point {
		// hear need more project functions
		var pp:Point3D = p.clone();
		
		//trace(this.tranceMatrix);
		Matrix3D.multiplyVector(this.tranceMatrix, pp);
		
		return new Point(pp.x, pp.y);
	}
	//------------------------------------------------------------------------------------- ADD FACE
	public function AddFace(face:Face) {
		if (this.faces[face.ID] == undefined) {
			this.faces.all.push(face);
			face.__index = this.faces.all.length - 1;
			if (this.screen!=undefined) {
				this.screen.AddFace(face);
			}
		}
	}
	public function DelFace(face:Object) {
		var faceid:String;
		if (face!=undefined && face.ID == undefined) {
			faceid = String(face);
		}else if(face.ID!=undefined){
			faceid = face.ID;
		}
		
		if (this.faces[faceid] != undefined) {
				var id:Number = this.faces[faceid].__index;
				this.faces.all = this.faces.all.slice(0, id).concat(this.faces.all.slice(id + 1));
				if (screen != undefined) {
					screen.RemoveFace(this.faces[faceid]);
				}
				delete this.faces[faceid];
		}
	}
	public function RemoveAllFace() {
		for (var i = 0; i < this.faces.all.length; i++) {
			if (screen != undefined) {
				screen.RemoveFace(this.faces.all[i]);
			}
		}
		this.faces = new Object();
		this.faces.all = new Array();
	}
}